package com.xy.shop.web.controller;

import com.github.pagehelper.Page;
import com.xy.shop.common.dtgrid.model.Pager;
import com.xy.shop.common.utils.WebUtil;
import com.xy.shop.system.entity.User;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class BaseController {


    @Autowired
    private WebUtil webUtil;

    public static Logger logger = LoggerFactory.getLogger(BaseController.class);
    protected Map<String, Object> getReturnPage(Pager pager , Page<Object> page, Object list){
        Map<String, Object> parameters=new HashMap<>();
        parameters.put("isSuccess", Boolean.TRUE);
        parameters.put("nowPage", pager.getNowPage());
        parameters.put("pageSize", pager.getPageSize());
        parameters.put("pageCount", page.getPages());
        parameters.put("recordCount", page.getTotal());
        parameters.put("startRecord", page.getStartRow());
        //列表展示数据
        parameters.put("exhibitDatas", list);
        return parameters;

    }

    protected Map<String,Object> getAppParams(Map<String,Object> parameters){
        if(parameters == null){
            parameters = new HashMap<>();
        }
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int appId = webUtil.getAppId(user.getLoginName());
        String appCode = webUtil.getAppCode(user.getLoginName());
        parameters.put("appId",appId);
        parameters.put("appCode", appCode);
        return parameters;
    }
}
